package applications.libraryForm.ui.lib;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.Observable;

import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.SwingConstants;
import javax.swing.table.AbstractTableModel;
import javax.swing.table.TableModel;

import org.objectweb.fractal.fraclet.annotations.Component;
import org.objectweb.fractal.fraclet.annotations.Interface;
import org.objectweb.fractal.fraclet.annotations.Requires;

import applications.libraryForm.fc.api.LibraryForm;

@Component(provides = {@Interface(name = "jPanelItf", signature = org.objectweb.fractal.swing.JPanelItf.class), @Interface(name = "jPanelAttributes", signature = org.objectweb.fractal.swing.JPanelAttributes.class), @Interface(name = "uicontainerservice", signature = ontocompo.api.UIContainerService.class), @Interface(name = "ontocompointrospection", signature = ontocompo.api.OntoCompoIntrospection.class)})

public class LibraryFormReturnedBooksPanel extends JPanel  implements  java.util.Observer, org.objectweb.fractal.swing.JPanelItf, org.objectweb.fractal.swing.JPanelAttributes, ontocompo.api.UIContainerService, ontocompo.api.OntoCompoIntrospection {

	private String uid;
	private org.objectweb.fractal.api.Component c;


	private JTable table;


	@Requires(name = "libraryformdirectoryservice")
	private applications.libraryForm.fc.api.LibraryFormDirectoryService libraryformdirectoryservice;

	@Requires(name = "libraryformdirectoryobservable")
	private ontocompo.api.Observable libraryformdirectoryobservable;


	public LibraryFormReturnedBooksPanel() {
		super();
		this.uid = "applications.libraryForm.ui.lib.LibraryFormReturnedBooksPanel";
		System.out.println(this.toString() + " created");
	}


	@Override
	public void init() {
		// TODO Auto-generated method stub
		this.libraryformdirectoryobservable.addObserver(this);
		this.setLayout(new BorderLayout());
		this.setBackground(Color.white);


	}

	protected JLabel createTd(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setFont(result.getFont().deriveFont(Font.PLAIN));
		result.setHorizontalAlignment(SwingConstants.LEFT);
		return result;
	}

	protected JLabel createTh(String txt)
	{
		JLabel result = new JLabel(txt);
		result.setHorizontalAlignment(SwingConstants.CENTER);
		return result;
	}

	@Override
	public JComponent getComponentItSelf() {
		// TODO Auto-generated method stub
		return this;
	}

	@Override
	public void update(Object o) {


		this.removeAll();
		this.setBackground(Color.white);

		JLabel lbl  = createTh("Returned Books");
		lbl.setAlignmentX(java.awt.Component.CENTER_ALIGNMENT);

		JPanel panelContenu = new JPanel(new BorderLayout());
		panelContenu.setBackground(Color.white);


		JPanel panelNorth = new JPanel();
		panelNorth.setBackground(Color.white);
		panelNorth.add(lbl);


		JPanel panelCenter = new JPanel();
		panelCenter.setBackground(Color.white);


		final String[] columnNames = this.libraryformdirectoryservice.getSummaryTitles();

		final Object[][] data = this.libraryformdirectoryservice.getReturnedBooks();


		TableModel dataModel = new AbstractTableModel() {
			public int getColumnCount() { return columnNames.length; }
			public int getRowCount() { return data.length;}
			public Object getValueAt(int row, int col) {
				return data[row][col];
			}
			public String getColumnName(int column) {return columnNames[column];}
			public Class getColumnClass(int col) {
				return getValueAt(0,col).getClass();
			}
			public void setValueAt(Object aValue, int row, int column) {
				data[row][column] = aValue;
			}
		};



		table = new JTable(dataModel);
		table.setPreferredScrollableViewportSize(new Dimension(860, 200));
		table.setFillsViewportHeight(true);
		table.setForeground(Color.BLACK);
		table.setRowHeight(25);

		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setViewportView(table);

		panelCenter.add(scrollPane);

		panelContenu.add(panelNorth, BorderLayout.NORTH);
		panelContenu.add(panelCenter, BorderLayout.CENTER);

		this.add(panelContenu, BorderLayout.NORTH);


		this.getParent().validate();  // pour rafraichir la page




	}


	public String toString() {
		return "LibraryFormReturnedBooksPanel";
	}




	@Override
	public org.objectweb.fractal.api.Component getFractalComponent() {
		// TODO Auto-generated method stub
		return this.c;
	}

	@Override
	public String getUid() {
		// TODO Auto-generated method stub
		return this.uid;
	}

	@Override
	public void setFractalComponent(org.objectweb.fractal.api.Component c) {
		// TODO Auto-generated method stub
		this.c = c;

	}

	@Override
	public void setUid(String uid) {
		// TODO Auto-generated method stub
		this.uid = uid;
	}


	@Override
	public void update(Observable o, Object arg) {
		// TODO Auto-generated method stub

		LibraryForm bkk = this.libraryformdirectoryservice.getLibraryForm();

		if (bkk == null){
			return;
		}
		else { 
			this.update(bkk);
		}


	}
}
